<?php 

namespace application\controller;

require_once("application/view/View.php");
require_once("login/view/LoginView.php");
require_once("login/model/LoginModel.php");
require_once("register/view/RegisterView.php");
require_once("register/model/RegisterModel.php");
require_once("register/controller/RegisterController.php");
require_once("login/controller/LoginController.php");
require_once("posts/view/PostView.php");
require_once("posts/controller/PostController.php");
require_once("comments/controller/CommentController.php");
require_once("comments/view/CommentView.php");
require_once("common/model/BaseDAL.php");

class Application {

	/**
	 * @var \login\controller\LoginController $loginController
	 */
	private $loginController;
	/**
	 * @var \register\controller\RegisterController $registerController
	 */
	private $registerController;
	/**
	 * @var \posts\controller\PostController $postController
	 */
	private $postController;
	/**
	 * @var \comments\controller\CommentController $commentController
	 */
	private $commentController;
	/**
	 * @var \posts\view\PostView $postView
	 */
	private $postView;
	/**
	 * @var \application\view\View $view
	 */
	private $view;
	/**
	 * @var \common\model\BaseDAL $baseDAL
	 */
	private $baseDAL;
	
	public function __construct() {
		$baseDAL = new \common\model\BaseDAL();
		$loginModel = new \login\model\LoginModel($baseDAL);
		$loginView = new \login\view\LoginView($loginModel);
		$registerModel = new \register\model\RegisterModel($baseDAL);
		$registerView = new \register\view\RegisterView();
		$commentView = new \comments\view\CommentView($baseDAL);
		$this->postView = new \posts\view\PostView($baseDAL);
		$this->postController = new \posts\controller\PostController($this->postView, $baseDAL);
		$this->registerController = new \register\controller\RegisterController($registerView, $baseDAL);
		$this->loginController = new \login\controller\LoginController($loginView, $baseDAL);
		$this->commentController = new \comments\controller\CommentController($commentView, $baseDAL);
		$this->view = new \application\view\View($loginView, 
												$registerView, 
												$this->postView, 
												$commentView);
	}
	
	/**
	 * @return void
	 */
	public function runApp() {
		//run the login controller function runLogin to login
		$this->loginController->runLogin();
		//run the login controller function runRegister to register 
		$this->registerController->runRegister();
		//run the post controller function runPosts to add posts
		$this->postController->runPosts();
		//run the comment controller function runComments to add comments
		$this->commentController->runComments();
	
		//check if session is set 
		if ($this->loginController->checkSession()) {
			//check if user is adding a post
			if ($this->postView->addingPost()) {
				//return view to add posts
				return $this->view->getAddingPostPage();
			}
			else if($this->postView->showSinglePost()) {
				//get the id of the post user wants to 
				$postID = $this->postView->getPostId();
				//return single post view
				return $this->view->getSinglePostPage($postID);
			}
			//return the logged in view
			return $this->view->getLoggedInPage();
		}
		//if the user is logged out show the single post page
		else if($this->postView->showSinglePost()) {
			//get the id of the post user wants to 
			$postID = $this->postView->getPostId();
			//return single post view
			return $this->view->getSinglePostLoggedOut($postID);
		}
		//return the logged out page
		return $this->view->getLoggedOutPage();		
	}
}
